const tool = {

    makeId: () => {
        // 获取当前时间戳
        const timestamp = Date.now().toString(36);
        // 生成一个随机数
        const randomNum = Math.random().toString(36).substr(2, 9);
        // 组合时间戳和随机数生成唯一ID
        return timestamp + randomNum;
    },

    screen: (element) => {
        const isFull = !!(document.webkitIsFullScreen || document.mozFullScreen || document.msFullscreenElement || document.fullscreenElement);
        if (isFull) {
            if (document.exitFullscreen) {
                document.exitFullscreen();
            } else if (document.msExitFullscreen) {
                document.msExitFullscreen();
            } else if (document.mozCancelFullScreen) {
                document.mozCancelFullScreen();
            } else if (document.webkitExitFullscreen) {
                document.webkitExitFullscreen();
            }
        } else {
            if (element.requestFullscreen) {
                element.requestFullscreen();
            } else if (element.msRequestFullscreen) {
                element.msRequestFullscreen();
            } else if (element.mozRequestFullScreen) {
                element.mozRequestFullScreen();
            } else if (element.webkitRequestFullscreen) {
                element.webkitRequestFullscreen();
            }
        }
    },

    /**
     * 复制对象
     * @param object 对象
     * @returns {any} 复制的对象
     */
    copyObject: (object) => {
        return JSON.parse(JSON.stringify(object));
    },

    /**
     * 千分符
     * @param num 数字
     * @returns {string} 分隔后数字
     */
    groupSeparator: (num) => {
        num = num + '';
        if (!num.includes('.')) {
            num += '.';
        }
        return num.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
            return $1 + ',';
        }).replace(/\.$/, '');
    },

    openLink: (url, target = "_blank") => {
        let linkElement = document.createElement("a");
        linkElement.setAttribute("href", url);
        linkElement.setAttribute("target", target);
        linkElement.setAttribute("rel", "noreferrer noopener");
        linkElement.setAttribute("id", "external");
        let existingLink = document.getElementById("external");
        if (existingLink) {
            document.body.removeChild(existingLink);
        }
        document.body.appendChild(linkElement);
        linkElement.click();
        linkElement.remove();
    },

    /**
     * 路由扁平化
     * @param routes 路由
     * @param breadcrumb 子路由
     * @returns {*[]} 全新路由
     */
    flatAsyncRoutes: (routes, breadcrumb = []) => {
        let res = [];
        routes.forEach((route) => {
            const tmp = {...route};
            if (tmp.children) {
                let childrenBreadcrumb = [...breadcrumb];
                childrenBreadcrumb.push(route);
                let tmpRoute = {...route};
                tmpRoute.meta.breadcrumb = childrenBreadcrumb;
                delete tmpRoute.children;
                res.push(tmpRoute);
                let childrenRoutes = tool.flatAsyncRoutes(tmp.children, childrenBreadcrumb);
                childrenRoutes.map((item) => res.push(item));
            } else {
                let tmpBreadcrumb = [...breadcrumb];
                tmpBreadcrumb.push(tmp);
                tmp.meta.breadcrumb = tmpBreadcrumb;
                res.push(tmp);
            }
        });
        return res;
    },

    /**
     * 转换
     * @param routerMap 路由
     * @returns {*[]} 转换后的路由
     */
    filterAsyncRouter: (routerMap) => {
        const accessedRouters = [];
        routerMap.forEach((item) => {
            item.meta = item.meta ? item.meta : {};
            // 处理外部链接特殊路由
            if (item.meta.category === 'IFRAME') {
                item.meta.url = item.component;
            }
            // 处理外部链接特殊路由
            if (item.meta.category === 'LINK') {
                item.meta.url = item.component;
            }
            //MAP转路由对象
            const route = {
                path: item.path,
                name: item.name,
                meta: item.meta,
                redirect: item.redirect,
                children: item.children ? tool.filterAsyncRouter(item.children) : null,
                component: tool.loadComponent(item),
            };
            accessedRouters.push(route);
        });
        return accessedRouters;
    },

    modules: import.meta.glob('@/views/**/*.vue'),

    /**
     * 加载组件
     * @param route 路由
     * @returns {function(): Promise<*>} 组件
     */
    loadComponent: (route) => {
        if (route.meta?.category === 'IFRAME') {
            return () => import('@/layout/components/iframe.vue');
        } else if (route.component) {
            // 匹配 views 里面所有的 .vue 文件
            for (const path in tool.modules) {
                const dir = path.split('views/')[1].split('.vue')[0];
                if (dir === route.component || dir === route.component + '/index') {
                    return () => tool.modules[path]();
                }
            }
        }
        return () => import('@/layout/error/404/index.vue');
    },

    /**
     * 是否是方法
     * @param callback 参数
     * @returns {boolean} 是否是方法
     */
    isFunction: (callback) => {
        return typeof callback == "function";
    },

    /**
     * 比对两组数据是否一致
     * @param news
     * @param old
     * @returns {boolean}
     */
    compareArray: (news, old) => {
        let count = 0;
        const length = news.length;
        for (let i in news) {
            for (let j in old) {
                if (news[i] === old[j]) {
                    count++;
                }
            }
        }
        return count === length;
    },

}

export default tool;
